package core;

import branch.Branch;
import repository.Repository;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

public class JitCheckout {
    public static void checkout(String name) throws IOException {
        File dir = new File(Repository.getGitDir() + File.separator + "refs" + File.separator + "heads");
        boolean branchExist = false;
        File[] fs = dir.listFiles();
        for(File f : fs){
            if(f.getName().equals(name)) branchExist = true;
        }
        if(branchExist){
            if(Branch.getBranchName().equals(name)){
                System.out.println("Current branch is already target branch.");
            }else{
                File HEADfile = new File(Repository.getGitDir() + File.separator + "HEAD");
                FileWriter fileWriter = new FileWriter(HEADfile);
                fileWriter.write("ref: refs/heads/" + name);//修改HEAD文件内容
                fileWriter.close();
            }
        }else{
            System.out.println("Branch doesn't exist.");
        }

    }
}